// pages/myInfo/myInfo.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: getApp().globalData.username,
    url: getApp().globalData.url,
    infoId: '',
    infoType: 0,
    currDate: '',
    currTime: '',
    appointDate: '',
    appointTime: '',
    patientName: '',
    patientTel: '',
    currentId: -1,
    info: [],
    info1: [
      {
        infoId: 1,
        infoType: 1,
        currDate: '2020-12-20',
        currTime: '16:05:57',
        appointDate: '2020-12-23',
        appointTime: '下午',
        patientName: '钟婧',
        patientTel: '13797209248',
        state: '未读',
        assistantName: '',
        assistantTel: '',
        doctorName: '姜文飞',
        doctorTel: '18708643079'
      },
      {
        infoId: 2,
        infoType: 2,
        currDate: '2020-12-20',
        currTime: '14:43:35',
        appointDate: '2020-12-19',
        appointTime: '上午',
        patientName: '李雨辰',
        patientTel: '13707291248',
        state: '已读',
        assistantName: '',
        assistantTel: '',
        doctorName: '姜文飞',
        doctorTel: '18708643079'
      },
      {
        infoId: 3,
        infoType: 3,
        currDate: '2020-12-15',
        currTime: '14:43:26',
        appointDate: '2020-12-30',
        appointTime: '上午',
        patientName: '张峻宇',
        patientTel: '13707291248',
        state: '已读',
        assistantName: '',
        assistantTel: '',
        doctorName: '姜文飞',
        doctorTel: '18708643079'
      },
      {
        infoId: 4,
        infoType: 4,
        currDate: '2020-12-15',
        currTime: '14:43:14',
        appointDate: '2020-12-30',
        appointTime: '上午',
        patientName: '张峻宇',
        patientTel: '13707291248',
        state: '未读',
        assistantName: '徐静茹',
        assistantTel: '18727508188',
        doctorName: '姜文飞',
        doctorTel: '18708643079'
      }
    ],
    scrollHeight: 0,
    timer: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();

    this.getInfo()
    this.setData({
      username: getApp().globalData.username,
      url: getApp().globalData.url,
    })
    try {
      var ap = wx.getStorageSync('assInfo')
      this.setData({
        info: ap
      })
    } catch(e) { }

    var that = this;
    //动态scrollview
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scrollHeight: (windowHeight - 0 - 60) * 750 / windowWidth
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo()

    try {
      var ap = wx.getStorageSync('assInfo')
      this.setData({
        info: ap
      })
    } catch(e) { }

    var that = this
    that.data.timer = setInterval(function () {
      that.getInfo();
      console.log("轮播请求10秒触发一次");
    }, 10000) 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //清除计时器  即清除setInter
    clearInterval(this.data.timer)
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    //清除计时器  即清除setIntertimer
    clearInterval(that.data.timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        // console.info(res.windowHeight);
        let height = res.windowHeight;
        wx.createSelectorQuery().selectAll('.list_top').boundingClientRect(function (rects) {
          rects.forEach(function (rect) {
            that.setData({
              scrollHeight: height - rect.bottom
            });
          })
        }).exec();
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getInfo()
  },

  /**
   * 获取通知信息
   */
  getInfo: function () {
    var that = this
    wx.request({
      url: getApp().globalData.url + 'getAssistantInfoServlet',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        username: getApp().globalData.username
      },
      success: function (res) {
        if (res.data != false && res.data != "false" && res.data != "空") 
        {
          try {
            wx.setStorageSync("assInfo", res.data)
          } catch (e) { }
          that.setData({
            info: res.data
          })
        }
        else if(res.data == "空")
        {
          that.setData({
            info: null
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 滑动事件
   */
  backToStart: function(e) {
    if (this.data.currentId != e.currentTarget.dataset.idx && this.data.currentId != -1) {
      this.setData({
        scl: 0
      })
    }
    this.setData({
      currentId: e.currentTarget.dataset.idx
    })
  },

  /**
   * 助理点击一键已读，将所有未读消息设为已读
   */
  allReadBtnClick: function(e) {
    wx.request({
      url: getApp().globalData.url + 'assistantSetAllReadServlet',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        assistantTel: getApp().globalData.username
      },
      success: function (res) {
        if(res.data != 'false' && res.data != false && res.data != '')
        {
          wx.showToast({
            title: '已将所有消息设为已读',
            icon: 'none',
            duration: 2000
          })
          that.getInfo()
        }
        else{
          wx.showToast({
            title: '设置失败，请尝试重新点击一键已读',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络连接失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 跳转详细通知信息并把消息设置为已读
   */
  clickInfo: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var queryBean = JSON.stringify(that.data.info[index])
    var infoId = that.data.info[index].infoId

    wx.request({
      url: getApp().globalData.url + 'assistantSetReadServlet',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        tel: getApp().globalData.username,
        infoId: infoId
      },
      success: function (res) {
        if(res.data != 'false' && res.data != false && res.data != '')
        {
          var that = this
          //拿到点击的index下标
          var index = e.currentTarget.dataset.idx
          //将对象转为string
          var queryBean = JSON.stringify(that.data.info[index])
          wx.navigateTo({
            url: '../detailedInfo/detailedInfo?queryBean='+queryBean,
          })
        }
        else{
          wx.showToast({
            title: '查看通知详情失败，请尝试重新点击该通知',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络连接失败，请尝试重新点击该通知',
          icon: 'none',
          duration: 2000
        })
      }
    })

    wx.navigateTo({
      url: '../detailedInfo/detailedInfo?queryBean='+queryBean,
    })
  }
})